/*
 * 
 */
package com.flying.dcss.common.action;

import java.util.LinkedList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.SessionName;
import com.flying.dcss.common.bo.impl.ComAnswer;
import com.flying.dcss.common.bo.impl.ComQuestion;
import com.flying.dcss.common.service.impl.CommentService;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.flying.dcss.viewmodel.impl.CommentListViewModel;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class GetCommentAction.
 */
public class GetCommentAction extends ActionSupport {
	
	/** The comment service. */
	private CommentService commentService;
	
	/** The user service. */
	private UserService userService;

	/**
	 * Sets the comment service.
	 *
	 * @param commentService the new comment service
	 */
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	/**
	 * Sets the user service.
	 *
	 * @param userService the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		List<CommentListViewModel> models = new LinkedList<CommentListViewModel>();

		List<ComQuestion> questions = commentService.GetAllQuestion();

		if (questions != null) {
			for (ComQuestion item : questions) {

				// Get answer.
				ComAnswer answer = commentService.getAnswerByQuestionId(item
						.getId());

				DcssUser answerUser = null;
				if (answer != null) {
					answerUser = userService.getUserById(answer.getUserId());
				}

				CommentListViewModel model = new CommentListViewModel(item,
						answer, userService.getUserById(item.getUserId()),
						answerUser);

				models.add(model);
			}
		}

		// Write session.
		ServletActionContext.getRequest().setAttribute(
				SessionName.CommentListSession, models);

		return this.SUCCESS;
	}
}
